<?php
/**
 * プラグイン拡張クラス
 *
 * @author tanaka
 * @package View_Smarty
 * @subpackage Extend
 */
class View_Smarty_Extend extends View_Smarty
{
	/**
	 * @var View_Smarty_Plugin_Modifier
	 */
	protected $_modifier;
	
	/**
	 * @var View_Smarty_Plugin_Functions
	 */
	protected $_functions;
	
	/**
	 * assignされたテンプレート
	 * @var array
	 */
	protected $_tplAssigned = array();

	/**
	 * コンストラクタ
	 *
	 * @param string $tmplPath
	 * @param array $extraParams
	 * @return void
	 */
	public function __construct($tmplPath = null, $extraParams = array())
	{
		parent::__construct($tmplPath, $extraParams);

		$this->_modifier = View_Smarty_Plugin_Modifier::getInstance();
		$this->_functions = View_Smarty_Plugin_Functions::getInstance();

		//smartyに修飾詞を追加
		foreach(get_class_methods($this->_modifier) as $method){
			$this->_smarty->register_modifier($method, array(&$this->_modifier, $method));
		}
		
		//smartyに関数を追加
		foreach(get_class_methods($this->_functions) as $method){
			$this->_smarty->register_function($method, array(&$this->_functions, $method));
		}
	}
	
	/**
	 * (non-PHPdoc)
	 * @see View_Smarty::__set()
	 */
	public function __set($key, $val)
	{
		if(_DEBUG){
			$this->_tplAssigned[$key] = $val;
		}
		
		parent::__set($key, $val);
	}
	
	/**
	 * (non-PHPdoc)
	 * @see View_Smarty::assign()
	 */
	public function assign($spec, $value = null)
	{
		if(_DEBUG){
			if(is_array($spec)){
				$this->_tplAssigned[] = $spec;
			}
			else{
				$this->_tplAssigned[$spec] = $value;
			}
		}
		parent::assign($spec, $value);
	}
	
	/**
	 * (non-PHPdoc)
	 * @see View_Smarty::__unset()
	 */
	public function __unset($key)
	{
		if(_DEBUG && isset($this->_tplAssigned[$key])){
			unset($this->_tplAssigned[$key]);
		}
		
		parent::__unset($key);
	}
	
	/**
	 * (non-PHPdoc)
	 * @see View_Smarty::clearVars()
	 */
	public function clearVars()
	{
		if(_DEBUG){
			$this->_tplAssigned = array();
		}
		parent::clearVars();
	}
	
	/**
	 * assignされた変数を返します。
	 * 
	 * @return array
	 */
	public function getAllAssigned()
	{
		return $this->_tplAssigned;
	}
}